/*
 * extraMath.cpp
 *
 *  Created on: 24-nov.-2013
 *      Author: Axel
 */

#include "extraMath.h"

namespace math {

	/*todo:
	 * acos
	 * atan
	 * asin
	 * acosh
	 * asinh
	 * atanh
	 * 2log
	 * 10log
	 *
	 * error function
	 * complementary error
	 * gamma
	 * gamma-log
	 *
	 * integral
	 * afgeleide
	 */

	double exp_axel(int x) {
		return power_axel(E, x);
	}

	double exp2_axel(int x) {
			return power_axel(2, x);
	}

	double power_axel(double base, double power) {
		double result = 1;
		while (power > 0) {
			result*= base;
			power--;
		}

		return result;
	}

	double faculty_axel(double x) {
		double result = 1;

		while(x > 1) {
			result *= x;
			x--;
		}

		return result;
	}

	double cosh_axel(double x) {
		double result = power_axel(E,x) + power_axel(E,-x);

		return result/2;
	}

	double sinh_axel(double x) {
		double result = power_axel(E,x) - power_axel(E,-x);

		return result/2;
	}

	double tanh_axel(double x) {
		double num = 1 - power_axel(E, -2*x);
		double denum = 1 + power_axel(E, -2*x);
		return num/denum;
	}

	double sin_axel(double x) {
		controlAngle(x);
		int len = 11;
		double result = 0;
		while (len > 0) {
			result += power_axel(-1,len+1) * power_axel(x,(2*len-1)) / faculty_axel(2*len-1);
			len--;
		}
		return result;
	}

	double cos_axel(double x) {
		controlAngle(x);
		int len = 11;
		double result = 0;
		while (len >=0) {
			result += power_axel(-1,len) * power_axel(x,(2*len)) / faculty_axel(2*len);
			len--;
		}
		return result;
	}

	double tan_axel(double x) {
		controlAngle(x);
		double result = x + power_axel(x,3)/3 + (2*power_axel(x,5))/15 + (17*power_axel(x,7))/315;
		return result;
	}

	double controlAngle(double x) {
		while (x < 0)
			x+=2*PI;
		while (x>=(2*PI-0.0001))
			x-=2*PI;

		return x;
	}

}

